import { toShare } from '../../utils/requestApi.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    img: null,
    show: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    shareInfo(){
      wx.showLoading({ title: '获取中...' })
      toShare({
        Type: 1,
        Path: 'pages/home/home'
      }).then(res =>{
        wx.hideLoading();
        if (res.data.Status == 'Fail') wx.showToast({ icon: 'none', title: res.data.Message })
        else this.setData({ img: res.data.Result.Data[0].url, show: !this.data.show })
      })
    },

    onColse(){
      this.setData({ show: false })
    },
    
    saveImg(e){
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {
                console.log('授权成功')
              }
            })
          }
        }
      })
      let { path } = e.currentTarget.dataset;
      wx.showLoading({ title: '保存中...' })
      wx.downloadFile({
        url: path, 
        success: res => {
          if (res.statusCode === 200) {
            wx.hideLoading();
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: res => {
                wx.showToast({ icon: 'none', title: '保存成功' })
                // this.setData({ show: !this.data.show })
              },
              fail: res => {
                wx.showToast({ icon: 'none', title: '保存失败' })
                // this.setData({ show: !this.data.show })
              }
            })
          }
        }
      })
      
    }
  }
})
